package com.eco.fanliapp.utils;


import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

    public static final String DATE_FORMAT_YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_YYYYMMDD = "yyyy-MM-dd";
    public static final String DATE_FORMAT_YYYYMM = "yyyy-MM";

    /**
     * 获取当前时间时间厝
     * @return
     */
    public static Timestamp getNowTimestamp(){
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        return ts;
    }

    /** 获取今天 */
    public static Date getNowDate() {
        Calendar c = Calendar.getInstance();
        return c.getTime();
    }
    /**
     * @Title:returnStrs
     * @Description: 将时间类型Date转换为到日的字符串
     * @param date
     * @return
     * @return String
     */
    public static String date2Strday(Date date){
        return date2Str(date, DATE_FORMAT_YYYYMMDD);
    }
    /**
     * @Title:returnStrs
     * @Description: 将时间类型Date转换为到日的字符串
     * @param date
     * @return
     * @return String
     */
    public static String date2Strmonth(Date date){
        return date2Str(date, DATE_FORMAT_YYYYMM);
    }
    /**
     * @Title:returnData
     * @Description: 字符串转换成日期
     * @param dateStr
     * @return
     * @return Date
     */
    public static Date datestr2Date(String dateStr,String format){
        if (StringUtils.isNull(dateStr)) {
            return null;
        }
        if (StringUtils.isNull(format)) {
            format = DATE_FORMAT_YYYYMMDDHHMMSS;
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);//小写的mm表示的是分钟
            Date date = sdf.parse(dateStr);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 将时间类型Date转换为指定日期格式的字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String date2Str(Date date, String format) {
        if (date == null) {
            return null;
        }
        if (StringUtils.isNull(format)) {
            format = DATE_FORMAT_YYYYMMDDHHMMSS;
        }
        try {
            SimpleDateFormat sf = new SimpleDateFormat(format);
            return sf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 将长整型数字转换为日期格式的字符串
     *
     * @param time
     * @param format
     * @return
     */
    public static String long2DatestrByFormt(long time, String format) {
        if (time > 0L) {
            if (StringUtils.isNull(format)) {
                format = DATE_FORMAT_YYYYMMDDHHMMSS;
            }
            try {
                Date date = new Date(time);
                return date2Str(date, format);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    /**
     * 将日期格式的字符串转换为长整型
     *
     * @param date
     * @param format yyy-MM-dd HH:mm:ss
     * @return
     */
    public static long datestr2LongByFormt(String date, String format) {
        if (StringUtils.isNull(date)) {
            return 0L;
        }
        if (StringUtils.isNull(format)) {
            format = DATE_FORMAT_YYYYMMDDHHMMSS;
        }
        try {
            SimpleDateFormat sf = new SimpleDateFormat(format);
            return sf.parse(date).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0l;
    }
    /**
     * 将日期格式的字符串转为Timestamp
     * @param tsStr yyy-MM-dd HH:mm:ss
     * @return
     */
    public  static Timestamp datestr2Time(String tsStr) {
//        Timestamp ts = new Timestamp(System.currentTimeMillis());
        //	String tsStr = "2011-05-09 11:49:45";
        if (StringUtils.isNull(tsStr)) {
            return null;
        }
        try {
            Timestamp ts = Timestamp.valueOf(tsStr);
            return ts;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     *  Date类型转成TimeStamp
     * @param date
     * @return
     */
    public static Timestamp date2Time(Date date) {
        if (date == null) {
            return null;
        }
        try {
            Timestamp ts = new Timestamp(date.getTime());
            return ts;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * tTimeStamp类型转成Date
     * @param timeStamp
     * @return
     */
    public static Date time2Date(Timestamp timeStamp) {
//        Date date = new Date();
        try {
//			Date d = new Date(timeStamp.getTime());
            Date date = new Date(timeStamp.getTime());
            return date;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 对时间按天进行增减
     * @param date
     * @param day
     * @return
     */
    public static Date updateDayByDate(Date date, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, day);
        return cal.getTime();
    }
    /**
     * 对时间按月进行增减
     * @param date
     * @param month
     * @return
     */
    public static Date updateMonthByDate(Date date, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, month);
        return cal.getTime();
    }

    /**
     * 对时间按小时进行增减
     * @param date
     * @param hours
     * @return
     */
    public static Date updateHourByDate(Date date, int hours) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, hours);
        return cal.getTime();
    }
    /**
     * 对时间按分钟进行增减
     * @param date
     * @param min
     * @return
     */
    public static Date updateMinByDate(Date date, int min) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, min);
        return cal.getTime();
    }

    /**
     * 当前时间增加/较少周数后的时间
     * @param date
     * @param addDay
     * @return
     */
    public static Date updateWeekByDate(Date date,int addDay){
        //
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_WEEK, addDay);
        Date time = calendar.getTime();
        return time;
    }
    /**
     * 判断当前日期是星期几
     *
     * @return dayForWeek 判断结果  7，1、2、3、4、5、6、分别代表：周日、周一、周二、周三、周四、周五、周六
     * @Exception 发生异常
     */
    public static int todayForWeek() {

        return dayForWeek(new Date());
    }

    /**
     * 判断日期是星期几
     *
     * @return dayForWeek 判断结果  7，1、2、3、4、5、6、分别代表：周日、周一、周二、周三、周四、周五、周六
     * @Exception 发生异常
     */
    public static int dayForWeek(Date now) {
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(now);
            int dayForWeek = 0;
            if(c.get(Calendar.DAY_OF_WEEK) == 1){
                dayForWeek = 7;
            }else{
                dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
            }
            return dayForWeek;
        } catch (Exception e) {

        }
        return -1;
    }

    /**
     *  获取本周某一天
     * @param weekDay   weekDay值为：1、2、3、4、5、6、7，分别代表：周日、周一、周二、周三、周四、周五、周六
     * @return  返回本周某一天 （时分秒为当前时间时分秒）
     */
    public static Date getDayOfThisWeek(int weekDay){
        if (weekDay < 1 || weekDay > 7) {

            return null;
        }
        return getDayOfWeekByWhichDate(new Date(), weekDay);
    }

    /**
     *  获取某个时间对应的周的某一天
     *  *@param whichDate  时间
     * @param weekDay   weekDay值为：1、2、3、4、5、6、7，分别代表：周日、周一、周二、周三、周四、周五、周六
     * @return  返回某个时间对应的周的某一天 （时分秒为当前时间时分秒）
     */
    public static Date getDayOfWeekByWhichDate(Date whichDate,int weekDay){
        if (whichDate == null) {
            return null;
        }
        if (weekDay < 1 || weekDay > 7) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(whichDate);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK, weekDay);
        return calendar.getTime();
    }
    /**
     *   获取本周周日
     * @return  返回本周周日 （时分秒为当前时间时分秒）
     */
    public static Date getSundayOfThisWeek() {
        return getDayOfWeekByWhichDate(new Date(), Calendar.SUNDAY);
    }
    /**
     *  获取本周周一
     * @return  返回本周周一  （时分秒为当前时间时分秒）
     */
    public static Date getMondayOfThisWeek() {
        return getDayOfWeekByWhichDate(new Date(), Calendar.MONDAY);
    }

    /**
     *  获取上周周一
     * @return  返回上周周一
     */
    public static Date getMondayOfLastWeek() {
        Date lastWeek = updateWeekByDate(new Date(), -1);
        return getDayOfWeekByWhichDate(lastWeek,Calendar.MONDAY);

    }

    /** 获取上周周日 */
    /**
     *  获取上周周日
     * @return 返回上周周日
     */
    public static Date getSundayOfLastWeek() {
        Date lastWeek = updateWeekByDate(new Date(), -1);
        return getDayOfWeekByWhichDate(lastWeek,Calendar.SUNDAY);
    }

    /**
     *  获取当前时间间隔N周的周几
     * @param weekDay weekDay值为：1、2、3、4、5、6、7，分别代表：周日、周一、周二、周三、周四、周五、周六
     * @param poor 间隔
     * @return 当前时间间隔N周的周几
     */
    public static Date getDayOfPoorThisWeek(int weekDay,int poor) {
        if (weekDay < 1 || weekDay > 7) {
            return null;
        }
        Date poorWeek = updateWeekByDate(new Date(), poor);
        return getDayOfWeekByWhichDate(poorWeek,weekDay);
    }

    /**
     *  获取某个时间间隔N周的周几
     * @param whichDate 某个时间
     * @param weekDay weekDay值为：1、2、3、4、5、6、7，分别代表：周日、周一、周二、周三、周四、周五、周六
     * @param poor 间隔
     * @return 某个时间间隔N周的周几
     */
    public static Date getDayOfPoorWeekByWhichDate(Date whichDate,int weekDay,int poor) {
        if (whichDate == null) {
            return null;
        }
        if (weekDay < 1 || weekDay > 7) {
            return null;
        }
        Date poorWeek = updateWeekByDate(whichDate, poor);
        return getDayOfWeekByWhichDate(poorWeek,weekDay);
    }

    /**
     *  获取某天的0点
     * @param fDay
     * @return
     */
    public static Date getZeroStartTimeByDate(Date fDay) {
        if (fDay == null) {
            return  null;
        }
        return datestr2Date(date2Str(fDay, "yyyy-MM-dd 00:00:00"),DATE_FORMAT_YYYYMMDDHHMMSS);
    }
    /**
     * 获取某天的23：59：59时间
     * @param fDay
     * @return
     */
    public static Date getZeroEndTimeByDate(Date fDay) {
        if (fDay == null) {
            return  null;
        }
        return datestr2Date(date2Str(fDay, "yyyy-MM-dd 23:59:59"),DATE_FORMAT_YYYYMMDDHHMMSS);
    }

    /** 获取本月一号 */
    public static Date getFirstDayOfThisMonth() {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_MONTH,
                calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }
    /**
     * 获取本月最后一天
     */
    public static Date getLastDayOfThisMonth() {
        Calendar calendar = Calendar.getInstance();
        // 设置时间,当前时间不用设置
        // calendar.setTime(new Date());
        // 设置日期为本月最大日期
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        return calendar.getTime();
    }

    /** 获取两个时间的时间查 如1天2小时30分钟 */
    public static String getDatePoor(Date endDate, Date nowDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
         long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟"+ sec + "秒";
    }

    /**
     * @Title:getDatePoor
     * @Description: 返回日期差值
     * @param endDate
     * @param nowDate
     * @param flag(d：相差多少天；h：相差多少小时；m：相差多少分；空或者其他值则返回0)
     * @return
     * @return String
     */
    public static long getDatePoor(Date endDate, Date nowDate, String flag) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
//        // 计算差多少天
//        long day = diff / nd;
//        // 计算差多少小时
//        long hour = diff % nd / nh;
//        // 计算差多少分钟
//        long min = diff % nd % nh / nm;
//        // 计算差多少秒//输出结果
//        long sec = diff % nd % nh % nm / ns;
//        long result = 0;
        if("d".equals(flag)){
            return diff / nd;
        }else if("h".equals(flag)){
            return diff % nd / nh;
        }else if("m".equals(flag)){
            return diff % nd % nh / nm;
        }else if("s".equals(flag)){
            return diff % nd % nh % nm / ns;
        }
        return 0l;
    }

    //================20200303_祝超==========
    // 获得当天0点时间
    public static Date getTimesmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();


    }
    // 获得昨天0点时间
    public static Date getYesterdaymorning() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(getTimesmorning().getTime()-3600*24*1000);
        return cal.getTime();
    }
    // 获得当天近7天时间0点时间
    public static Date getWeekFromNow() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis( getTimesmorning().getTime()-3600*24*1000*7);
        return cal.getTime();
    }
    // 获得当天近30天时间0点时间
    public static Date getMonthFromNow() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH) - 30, 0, 0, 0);
//        long tt = calendar.getTime().getTime() / 1000;
        return calendar.getTime();
    }
    // 获得当天24点时间
    public static Date getTimesnight() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    // 获得本周一0点时间
    public static Date getTimesWeekmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }

    // 获得本周日24点时间
    public static Date getTimesWeeknight() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getTimesWeekmorning());
        cal.add(Calendar.DAY_OF_WEEK, 7);
        return cal.getTime();
    }

    // 获得本月第一天0点时间
    public static Date getTimesMonthmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    // 获得本月最后一天24点时间
    public static Date getTimesMonthnight() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        return cal.getTime();
    }

    //上月初0点时间
    public static Date getLastMonthStartMorning() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getTimesMonthmorning());
        cal.add(Calendar.MONTH, -1);
        return cal.getTime();
    }

    //本季度开始0点时间
    public static Date getCurrentQuarterStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 当前季度的结束时间，即2012-03-31 23:59:59
     *
     * @return
     */
    public static Date getCurrentQuarterEndTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getCurrentQuarterStartTime());
        cal.add(Calendar.MONTH, 3);
        return cal.getTime();
    }

    //本年开始0点时间
    public static Date getCurrentYearStartTime() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.YEAR));
        return cal.getTime();
    }

    //本年结束24点时间
    public static Date getCurrentYearEndTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getCurrentYearStartTime());
        cal.add(Calendar.YEAR, 1);
        return cal.getTime();
    }

    //上年开始点时间
    public static Date getLastYearStartTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getCurrentYearStartTime());
        cal.add(Calendar.YEAR, -1);
        return cal.getTime();
    }



    public static void main(String[] args) {
        System.out.println(long2DatestrByFormt(1583823652971l,null));
//        System.out.println("当天2点时间：" + getMonthFromNow().getTime());

    }

}
